package com.txby.platformApi.component.web;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformCore.entity.SysChinaRegion;

/**
 * 试卷
 */
@RestController
@RequestMapping("/sys/china")
public class ChinaRegionController  extends CommonController {
	
	/**
	 * 所有试卷列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:china:list")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="SELECT * FROM "+this.mBaseDao.tableName(SysChinaRegion.class) ;
	/*	if(getUserId() != Constant.SUPER_ADMIN){
//			params.put("createUserId", getUserId());
			 join=getJoinSqlBracket();
			 sql+=" and name in "+getJoinSqlBracket();
		}*/
		String regionName=params.get("regionName")==null?"":params.get("regionName").toString();
		if(!ChkUtil.isEmptyAllObject(regionName)){
			if (regionName.contains("%") || regionName.contains("_")) {
				regionName = regionName.trim().replace("%", "\\%").replace("_", "\\_");
				regionName = "%" + regionName + "%";
				sql += " where regionName like #{pojo.regionName} escape '\\'";
				params.put("regionName", "%" + regionName.trim() + "%");
			} else {
				regionName = "%" + regionName.trim() + "%";
				sql += " where regionName like #{pojo.regionName} ";
				params.put("regionName", "%" + regionName.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by id_ ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
 
	
	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:china:info")
	public R info(@PathVariable("id") Long id){
		String sql="SELECT * FROM  "+this.mBaseDao.tableName(SysChinaRegion.class)+" WHERE id_="+id+"" ;
		Map china =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("china", china);
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@RequestMapping("/save")
	@RequiresPermissions("sys:china:save")
	public R save(@RequestBody SysChinaRegion china){
		ValidatorUtils.validateEntity(china, AddGroup.class);
		String sql="select * from "+this.mBaseDao.tableName(SysChinaRegion.class)+" where regionName='"+china.getRegionName()+"'";
		SysChinaRegion old=(SysChinaRegion) this.mBaseDao.findBySqlUniqueObject(sql, SysChinaRegion.class, null);
		 if(old!=null){
			 return R.ok().error("地区名称重复");
		 }
			this.mBaseDao.saveObject(china);
		 
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@RequestMapping("/update")
	@RequiresPermissions("sys:china:update")
	public R update(@RequestBody  SysChinaRegion china){
		ValidatorUtils.validateEntity(china, AddGroup.class);
		   String sql="select * from "+this.mBaseDao.tableName(SysChinaRegion.class)+" where regionName='"+china.getRegionName()+"' and id_!="+china.getId_()+" ";
		SysChinaRegion old=(SysChinaRegion) this.mBaseDao.findBySqlUniqueObject(sql, SysChinaRegion.class, null);
		 if(old!=null){
			 return R.ok().error("地区名称重复");
		 }else{
			 old=(SysChinaRegion) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(SysChinaRegion.class)+" where   id_="+china.getId_(), SysChinaRegion.class, null);
			 this.mBaseDao.updateObject(china);
		 }
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:china:delete")
	public R delete(@RequestBody Long[] ids){
		 for(Long id:ids){
		    	this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(SysChinaRegion.class)+" where id_="+id+"", null);
		  }
			return R.ok();
	}
	
	
}
